<template>
<div class="main_box">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb>
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/user' }">用户管理</el-breadcrumb-item>
        <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
        <el-col :span="7">
            <el-row>
                <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable></el-input>
                <el-button slot="append" icon="el-icon-search"></el-button>
            </el-row>
        </el-col>
        <el-col :span="1"></el-col>
        <el-col :span="4">
            <el-button @click="DialogVisible" type="primary">添加用户</el-button>
        </el-col>
        <!-- 用户列表区域 -->
        <el-table :data="userlist" border stripe>
            <el-table-column label="#" type="index"></el-table-column>
            <!-- <el-table-column label="姓名" prop="username"></el-table-column> -->
            <el-table-column label="邮箱" prop="email"></el-table-column>
            <el-table-column label="电话" prop="mobile"></el-table-column>
            <el-table-column label="角色" prop="role_name"></el-table-column>
            <el-table-column label="状态" prop="mg_state">
                <template>
                    <!-- {{scope.row}} -->
                    <el-switch v-model="userlist.mg_state"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <!-- 修改按钮 -->
                    <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog"></el-button>
                    <!-- 删除按钮 -->
                    <el-button type="danger" icon="el-icon-edit" size="mini" @click="removeUserById(scope.row.id)"></el-button>
                    <!-- 分配角色按钮 -->
                    <el-tooltip effect="dark" content="分配角色" placement="top">
                        <el-button type="warning" icon="el-icon-edit" size="mini" @click="setRole(scope.row)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页区域 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" 
        :current-page="queryInfo.pagenum" :page-sizes="[1,2,5,10]" :page-size="queryInfo.pagesize" 
        layout="total,sizes,prev,pager,next,jumper" :total="50"></el-pagination>

        <!-- 添加用户的对话框 -->
        <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
            <!-- 内容主体区域 -->
            <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addForm.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="addForm.password" type="password"></el-input>
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                    <el-input v-model="addForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <el-input v-model="addForm.mobile"></el-input>
                </el-form-item>
            </el-form>
            <!-- 底部区域 -->
            <span slot="footer">
                <el-button @click="addDialogVisible=false">取消</el-button>
                <el-button type="primary" @click="addUser">确定</el-button>
            </span>
        </el-dialog>

        <!-- 修改用户的对话框 -->
        <el-dialog title="修改用户信息" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
            <el-form :model="editForm" ref="editFormRef" :rules="editFormRules">
                <el-form-item label="用户名">
                    <!-- v-model 数据双向绑定，绑定到 editForm 对象中  -->
                    <el-input v-model="editForm.username" disabled></el-input>
                </el-form-item>
                <!-- prop 使用具体的校验规则 -->
                <el-form-item label="邮箱" prop="email">
                    <!-- v-model 数据双向绑定，绑定到 editForm 对象中  -->
                    <el-input v-model="editForm.email"></el-input>
                </el-form-item>
                <el-form-item label="手机" prop="mobile">
                    <!-- v-model 数据双向绑定，绑定到 editForm 对象中  -->
                    <el-input v-model="editForm.mobile"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="editUserInfo">确 定</el-button>
            </span>
        </el-dialog>

        <!-- 分配角色的对话框 -->
        <el-dialog title="分配角色" :visible.sync="setRoleDialogVisible" width="50%" @close="setRoleDialogClosed">
            <div>
                <el-form :model="userInfo" label-widtd="80px">
                    <el-form-item label="当前的用户">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="当前的角色">
                        <el-input v-model="userInfo.role_name" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="分配新角色">
                        <el-select v-model="selectedRoleId" placeholder="请选择" style="width:100%">
                            <el-option v-for="item in rolesList" :key="item.id" :label="item.roleName" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="setRoleDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveRoleInfo">确 定</el-button>
            </span>
        </el-dialog>
    </el-card>

</div>
</template>

<script>
export default {
    name: 'User',
    data() {
        // 验证邮箱的规则
        var checkEmail = (rule, value, callback) => {
            const regEmail = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/

            if (regEmail.test(value)) {
                // 合法
                return callback()
            }
            callback(new Error('请输入合法的邮箱'))
        }
        // 验证手机号的规则
        var checkMobile = (rule, value, callback) => {
            const regMobile = /^[1][3,4,5,7,8][0-9]{9}$/

            if (regMobile.test(value)) {
                // 合法
                return callback()
            }
            callback(new Error('请输入合法的手机'))
        }

        return {
            userlist: [
                { username: '小王', email: '2381386908@qq.com', mobile: '13183144941', role_name: '管理员', mg_state: true },
                { username: '小明', email: '2381386908@qq.com', mobile: '13183144941', role_name: '管理员', mg_state: false },
                { username: '小李', email: '2381386908@qq.com', mobile: '13183144941', role_name: '管理员', mg_state: true },
            ],
            currenList:[],
            //获取用户列表的参数对象
            queryInfo: {
                query: '',
                //当前页数
                pagenum: 1,
                //当前每页显示多少条数据
                pagesize: 10,
            },
            // 控制添加用户对话框的显示与隐藏
            addDialogVisible: false,
            addForm: {
                username: '',
                password: '',
                email: '',
                mobile: '',
            },
            addFormRules: {
                // username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '用户名长度在 3 - 10 个字符之间', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 10, message: '密码长度在 6 - 12 个字符之间', trigger: 'blur' }
                ],
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { validator: checkEmail, trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机', trigger: 'blur' },
                    { validator: checkMobile, trigger: 'blur' }
                ]
            },
            //控制编辑面板的显示
            editDialogVisible: false,
            editForm: {
                username: '',
                email: '',
                mobile: '',
            },
            // 修改表单的验证规则对象
            editFormRules: {
                email: [
                    { required: true, message: '请输入邮箱', trigger: 'blur' },
                    { validator: checkEmail, trigger: 'blur' }
                ],
                mobile: [
                    { required: true, message: '请输入手机', trigger: 'blur' },
                    { validator: checkMobile, trigger: 'blur' }
                ]
            },
            //分配角色面板是否显示
            setRoleDialogVisible: false,
            userInfo: {
                username: '',
                role_name: '',
            },
            rolesList:[
                {id:'1',roleName:'管理员'},
                {id:'2',roleName:'员工'}
            ]
        }
    },
    methods: {

        handleSizeChange(newsize) {
            console.log(newsize);
            this.queryInfo.pagesize = newsize;
        },
        handleCurrentChange(newpage) {
            console.log(newpage);
            this.queryInfo.pagenum = newpage;
            this.currenList=newpage
        },
        //编辑新用户
        DialogVisible() {
            this.addDialogVisible = true;
        },
        //确认添加新用户
        addUser() {
            this.addDialogVisible = false;
            this.$message.success('添加成功')
        },
        //关闭
        addDialogClosed() {
            this.$refs.addFormRef.resetFields();
            this.addDialogVisible = false;
        },
        //编辑用户面板
        showEditDialog() {
            this.editDialogVisible = true;
        },
        editUserInfo() {
            this.$refs.editFormRef.validate(valid => {
                if (valid) {
                    this.$message.success('修改信息成功')
                } else {
                    this.$message.error('输入错误')
                }
            })
            this.editDialogVisible = false;
        },
        //修改表单后，关闭应该重置，
        editDialogClosed() {
            this.$refs.editFormRef.resetFields();
        },
        //删除用户
        removeUserById(id) {
            console.log(id)
            // 弹框询问用户是否删除数据
            const confirmRes = this.$confirm(
                '此操作将永久删除该用户, 是否继续?',
                '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }
            ).catch(err => err) // 捕获了用户的取消行为的异常，下面正常输出 cancel
        },
        //分配角色
        setRole(userInfo) {
            this.userInfo = userInfo
            this.setRoleDialogVisible=true
        },
        setRoleDialogClosed() {

        }
    }
}
</script>

<style scoped>
.main_box {
    margin: 0px;
    padding: 0px;
}

.el-breadcrumb {
    padding: 10px 10px;
    font-size: 16px;
}

.el-card {
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15 green, blue, alpha);
}
</style>
